
#include "usart.h"

UsartInterrupt *interrupt_target[5] = {0, 0, 0, 0, 0};


extern "C" {

  void USART1_IRQHandler(void)
  {  
    if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) == SET ||
        USART_GetFlagStatus(USART1, USART_FLAG_NE) == SET ||
        USART_GetFlagStatus(USART1, USART_FLAG_FE) == SET ||
        USART_GetFlagStatus(USART1, USART_FLAG_PE) == SET)
    {
      // Remove bad byte
      USART_ReceiveData(USART1);
    }

    if(interrupt_target[1] && USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
      (interrupt_target[1]->*&UsartInterrupt::usartInterrupt)(USART_ReceiveData(USART1));
    }
  }

  void USART2_IRQHandler(void)
  {  
    if (USART_GetFlagStatus(USART2, USART_FLAG_ORE) == SET ||
        USART_GetFlagStatus(USART2, USART_FLAG_NE) == SET ||
        USART_GetFlagStatus(USART2, USART_FLAG_FE) == SET ||
        USART_GetFlagStatus(USART2, USART_FLAG_PE) == SET)
    {
      // Remove bad byte
      USART_ReceiveData(USART2);
    }

    if(interrupt_target[2] && USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) {
      (interrupt_target[2]->*&UsartInterrupt::usartInterrupt)(USART_ReceiveData(USART2));
    }
  }

  void USART3_IRQHandler(void)
  {  
    if (USART_GetFlagStatus(USART3, USART_FLAG_ORE) == SET ||
        USART_GetFlagStatus(USART3, USART_FLAG_NE) == SET ||
        USART_GetFlagStatus(USART3, USART_FLAG_FE) == SET ||
        USART_GetFlagStatus(USART3, USART_FLAG_PE) == SET)
    {
      // Remove bad byte
      USART_ReceiveData(USART3);
    }

    if(interrupt_target[3] && USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) {
      (interrupt_target[3]->*&UsartInterrupt::usartInterrupt)(USART_ReceiveData(USART3));
    }
  }

  void UART4_IRQHandler(void)
  {  
    if (USART_GetFlagStatus(UART4, USART_FLAG_ORE) == SET ||
        USART_GetFlagStatus(UART4, USART_FLAG_NE) == SET ||
        USART_GetFlagStatus(UART4, USART_FLAG_FE) == SET ||
        USART_GetFlagStatus(UART4, USART_FLAG_PE) == SET)
    {
      // Remove bad byte
      USART_ReceiveData(UART4);
    }

    if(interrupt_target[4] && USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) {
      (interrupt_target[4]->*&UsartInterrupt::usartInterrupt)(USART_ReceiveData(UART4));
    }
  }

  void UART5_IRQHandler(void)
  {  
    if (USART_GetFlagStatus(UART5, USART_FLAG_ORE) == SET ||
        USART_GetFlagStatus(UART5, USART_FLAG_NE) == SET ||
        USART_GetFlagStatus(UART5, USART_FLAG_FE) == SET ||
        USART_GetFlagStatus(UART5, USART_FLAG_PE) == SET)
    {
      // Remove bad byte
      USART_ReceiveData(UART5);
    }

    if(interrupt_target[5] && USART_GetITStatus(UART5, USART_IT_RXNE) != RESET) {
      (interrupt_target[5]->*&UsartInterrupt::usartInterrupt)(USART_ReceiveData(UART5));
    }
  }
}

